from django.shortcuts import render
from django.views import View
from apps.areas.models import Area
from django.http import JsonResponse
class AreaView(View):
    def get(self,request):
        # 1获取省份
        provinces=Area.objects.filter(parent=None)#这是queryset对象，不能直接转换为json
        # 2序列化
        province_list=[]
        for province in provinces:
            province_list.append({'id':province.id,'name':province.name})
        # 3返回响应
        return JsonResponse({'code':0,'errmsg':'OK','province_list':province_list})
class SubAreaView(View):
    def get(self,request,id):
        # 1获取省份id、市的id
        up_level=Area.objects.get(id=id)
        down_level=up_level.subs.all()
        data_list=[]
        for item in down_level:
            data_list.append({'id':item.id,'name':item.name})
        return JsonResponse({'code':0,'errmsg':'OK','sub_data':{'subs':data_list}})
